﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class CPBZXProperty : BasePage
    {
        public const string LEVEL = "level";
        public const string VPATH = "vpath";
        protected int level = -1;
        protected string vpath = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                object objlevel = Cache.Get(LEVEL);
                if (objlevel == null)
                {
                    TreeNode root = initRoot();
                }
                else
                {
                    level = (int)objlevel;
                    vpath = (string)Cache.Get(VPATH);
                    rePopulateNode(level, vpath);
                    level = -1;
                    vpath = null;
                    Cache.Remove(LEVEL);
                }
            }
        }

        private TreeNode initRoot()
        {
            tvWL.Nodes.Clear();
            TreeNode root = new TreeNode();
            root.Text = "产品线";
            root.Value = "root";
            root.PopulateOnDemand = false;
            root.NavigateUrl = string.Format("CS_CPX_Query.aspx?vpath=root");
            root.ImageUrl = "../images/Icon/home.png";
            root.ExpandAll();

            tvWL.Nodes.Add(root);
            populateNode(root);

            return root;
        }

        private void rePopulateNode(int level, string vPath)
        {
            populateNode(null);
        }

        private void loadCPX(TreeNode node)
        {
            using (CS_CPXBLL cBLL = new CS_CPXBLL())
            {
                List<CS_CPX> items = cBLL.GetAll();
                node.ChildNodes.Clear();

                foreach (CS_CPX item in items)
                {
                    TreeNode child = new TreeNode();
                    child.Text = string.Format("{0} {1}", item.BH.Trim(), item.MC);
                    child.Value = item.BH;
                    child.PopulateOnDemand = true;
                    child.NavigateUrl = string.Format("CS_CPHXC_Query.aspx?cpxbh={0}", child.Value);
                    child.ImageUrl = "../images/Icon/tools.png";

                    node.ChildNodes.Add(child);

                    if (vpath != null && vpath.StartsWith(child.ValuePath))
                    {
                        child.ExpandAll();
                        populateNode(child);
                    }
                }
            }
        }

        private void loadHXC(string cpxbh, TreeNode node)
        {
            node.ChildNodes.Clear();
            using (CS_CPMCBLL cBLL = new CS_CPMCBLL())
            {
                List<CS_CPMC> items = cBLL.GetList(t => t.CPXBH == cpxbh);

                foreach (CS_CPMC item in items)
                {
                    TreeNode child = new TreeNode();
                    child.Text = string.Format("{0} {1}", item.BH.Trim(), item.MC);
                    child.Value = item.BH;
                    child.PopulateOnDemand = true;
                    child.NavigateUrl = string.Format("ZL_CP_Query.aspx?hxcbh={0}&cpxbh={1}", child.Value, item.CPXBH);
                    child.ImageUrl = "../images/Icon/bullets.png";

                    node.ChildNodes.Add(child);

                    if (vpath != null && vpath.StartsWith(child.ValuePath))
                    {
                        child.Expanded = true;
                        populateNode(child);
                    }
                }
            }
        }

        private void loadCP(string cpxbh, string hxcbh, TreeNode node)
        {
            node.ChildNodes.Clear();
            using (ZL_CPBLL cBLL = new ZL_CPBLL())
            {
                List<ZL_CP> items = cBLL.GetList(t => t.CPXBH == cpxbh && t.CPMCBH == hxcbh);
                foreach (ZL_CP item in items)
                {
                    TreeNode child = new TreeNode();
                    child.Text = string.Format("{0} {1}", item.BH.Trim(), item.CPMC);
                    child.Value = item.BH;
                    child.PopulateOnDemand = false;
                    child.NavigateUrl = string.Format("CPBZX_Query.aspx?cpid={0}&cpxbh={1}&hxcbh={2}", item.ID, cpxbh, hxcbh);
                    child.ImageUrl = "../images/Icon/bullet_black.png";
                    node.ChildNodes.Add(child);
                }
            }
        }

        protected void tvWL_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (IsCallback)
            {
                populateNode(e.Node);
            }
        }

        protected void tvWL_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            populateNode(e.Node);
        }

        protected void UpdatePanel1_Load(object sender, EventArgs e)
        {
            string target = Request.Form["__EVENTTARGET"];
            if (("btnRefresh".Equals(target)))
            {
                string args = Request.Form["__EVENTARGUMENT"];
                string a = "args: " + args;
                TreeNode root = initRoot();
                loadCPX(root);
            }
        }

        private void populateNode(TreeNode node)
        {
            if (node == null)
            {
                initRoot();
                return;
            }
            switch (node.Depth)
            {
                case 0:
                    loadCPX(node);
                    break;
                case 1:
                    loadHXC(node.Value, node);
                    break;
                case 2:
                    string[] values = node.ValuePath.Split('/');
                    loadCP(values[1], node.Value, node);
                    break;
            }
        }
    }
}